﻿#include "EllipseGraphicsItem.h"
#include "GraphicsDef.h"
#include <QGraphicsScene>
#include <QPainter>

namespace OfUI {

	EllipseGraphicsItem::EllipseGraphicsItem(QGraphicsItem *parent)
		: RectGraphicsItem(parent)
	{
		setType(ItemType::ELLIPSE);
	}

	EllipseGraphicsItem::~EllipseGraphicsItem()
	{
	}

	BaseGraphicsItem::MouseDragType EllipseGraphicsItem::select(const QPointF& point, int* selType /*= nullptr*/) const
	{
		MouseDragType eDragType = DRAG_NONE;
		SelectType eSelectType = SELECT_NONE;
		QMap<SelectType, QPointF> dragPointMap = getDragSelectPoint(m_rect);

		for (auto iter = dragPointMap.begin(); iter != dragPointMap.end(); ++iter)
		{
			double dDistance = ((iter.key() == SELECT_CENTER) ? 5 : 3) / getViewScale();
			QPointF dragPoint = iter.value();
			QRectF pointRect(dragPoint - QPointF(dDistance, dDistance), dragPoint + QPointF(dDistance, dDistance));

			if (pointRect.contains(point))
			{
				eDragType = DRAG_EDIT;
				eSelectType = iter.key();
				break;
			}
		}

		if (SELECT_NONE == eSelectType)
		{
			// 按照椭圆公式判断点是否在椭圆内
			double A = qAbs(m_rect.width()) / 2;
			double B = qAbs(m_rect.height()) / 2;
			// 将坐标移动到
			QPointF p = point - m_rect.center();

			double C = pow(p.x(), 2) / pow(A, 2) + pow(p.y(), 2) / pow(B, 2);
			// 判断鼠标在椭圆内部
			if (C <= 1.0)
			{
				eDragType = DRAG_MOVE;
				eSelectType = SELECT_ALL;
			}
		}

		if (selType)
		{
			*selType = eSelectType;
		}
		return eDragType;
	}

	void EllipseGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget /* = nullptr */)
	{
		Q_UNUSED(option);
		Q_UNUSED(widget);

		if (this->scene())
		{
			painter->save();
			painter->setRenderHint(QPainter::Antialiasing);

			// 通过反向缩放painter实现矢量绘制效果
			double dViewScale = getViewScale();
			painter->scale(1 / dViewScale, 1 / dViewScale);

			// painter缩小，则绘制坐标放大，保证显示位置不变
			QRectF scaleRect = QRectF(m_rect.topLeft() * dViewScale, m_rect.bottomRight() * dViewScale);

			// 绘制椭圆
			painter->setPen(getPen(getState()));
			painter->drawEllipse(scaleRect);

			// 绘制中心十字
			double dRaduis = 5;
			if (qAbs(scaleRect.width()) >= 2 * dRaduis && qAbs(scaleRect.height()) >= 2 * dRaduis)
			{
				painter->drawLine(scaleRect.center() + QPointF(-dRaduis, 0), scaleRect.center() + QPointF(dRaduis, 0));
				painter->drawLine(scaleRect.center() + QPointF(0, -dRaduis), scaleRect.center() + QPointF(0, dRaduis));
			}

			// 绘制选择点
			if (STATE_SELECT == getState())
			{
				// 绘制矩形
				painter->save();
				QPen dashLinePen = painter->pen();
				dashLinePen.setStyle(Qt::DashLine);
				painter->setPen(dashLinePen);
				painter->drawRect(scaleRect);
				painter->restore();

				QPointF radiusPoint(3, 3);
				QMap<SelectType, QPointF> dragPointMap = getDragSelectPoint(scaleRect);
				dragPointMap.remove(SELECT_CENTER);

				for (auto iter = dragPointMap.begin(); iter != dragPointMap.end(); ++iter)
				{
					QPointF point = iter.value();
					painter->drawRect(QRectF(point - radiusPoint, point + radiusPoint));
				}
			}
			painter->restore();
		}
	}

}
